import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, Like } from 'typeorm';
import { CityEntity } from './entities/city.entity';
import { CreateCityDto } from './dto/create-city.dto';

@Injectable()
export class CityService {
  constructor(
    @InjectRepository(CityEntity)
    private cityRepo: Repository<CityEntity>,
  ) {}

  async findAll(): Promise<CityEntity[]> {
    return this.cityRepo.find();
  }

  async searchByName(keyword: string): Promise<CityEntity[]> {
    return this.cityRepo.find({ where: { name: Like(`%${keyword}%`) } });
  }

  async create(dto: CreateCityDto): Promise<CityEntity> {
    const city = this.cityRepo.create(dto);
    return this.cityRepo.save(city);
  }
}
